package com.woniuxy.controller.goodsGeneralAndSmallAndBrand;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.demo.Result;
import com.woniuxy.entity.GoodsSmall;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.AddGoodsSmallParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.DeleteSmallParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.ListGoodsSmallParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.UpdateGoodsSmallParam;
import com.woniuxy.service.GoodsSmallService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品小类 前端控制器
 * </p>
 *
 * @author 范腾龙
 * @since 2021-1-1
 * 关于商品小类信息管理的接口
 */
@RestController
@Api(tags = "关于商品小类信息管理的接口")
@RequestMapping("/goods-small")
public class GoodsSmallController {

    @Autowired
    private GoodsSmallService goodsSmallService;



    /**
     * 根据大类id查询小类信息
     *
     * @param id 大类信息id
     * @return 根据大类id查询到的小类信息集合
     */
    @GetMapping("/listSmallByGeneralId/{id}")
    @ApiOperation("根据大类id查询小类信息")
    public Result listSmallByGeneralId(@PathVariable("id") Integer id) {
        List<GoodsSmall> goodsSmallList = goodsSmallService.list(new QueryWrapper<GoodsSmall>().eq("goods_general_id", id));
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsSmallList);
    }


    /**
     * 新增小类信息后端接口
     *
     * @param bindingResult      参数验证
     * @param addGoodsSmallParam 前端传递的新增小类信息
     * @return 返回给前端的新增小类信息
     */
    @PostMapping("/addSmallInfo")
    @ApiOperation("新增小类信息")
    public Result addSmallInfo(@Validated @RequestBody AddGoodsSmallParam addGoodsSmallParam, BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        return Result.newInstance().setCode(200).setMsg("新增成功")
                .setData(goodsSmallService.saveSmallInfo(addGoodsSmallParam));
    }


    /**
     * 分页查询小类信息后端接口
     *
     * @param bindingResult       参数验证
     * @param listGoodsSmallParam 前端传递的小类分页信息
     * @return 返回给前端的分页和模糊查询后的小类信息集合
     */
    @PostMapping("/pageSmallInfo")
    @ApiOperation("分页查询小类信息")
    public Result pageSmallInfo(@Validated @RequestBody ListGoodsSmallParam listGoodsSmallParam, BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsSmallService.pageFind(listGoodsSmallParam));
    }


    /**
     * 修改小类信息后端接口
     *
     * @param bindingResult         参数验证
     * @param updateGoodsSmallParam 前端传递的小类修改的信息
     * @return 返回给前端的小类修改后的信息
     */
    @PostMapping("/updateSmallInfo")
    @ApiOperation("修改小类信息")
    public Result updateSmallInfo(@Validated @RequestBody UpdateGoodsSmallParam updateGoodsSmallParam, BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        return Result.newInstance().setCode(200).setMsg("修改成功")
                .setData(goodsSmallService.updateSmallInfo(updateGoodsSmallParam));
    }


    /**
     * 批量删除小类信息后端接口
     *
     * @param bindingResult    参数验证
     * @param deleteSmallParam 前段传递的要删除的小类信息的id集合
     * @return
     */
    @PostMapping("/removeSmallInfo")
    @ApiOperation("批量删除小类信息")
    public Result removeSmallInfo(@Validated @RequestBody DeleteSmallParam deleteSmallParam, BindingResult bindingResult) {
        //参数验证
        ValidatedUtil.ValidatedException(bindingResult);
        if (goodsSmallService.deleteGoodsSmall(deleteSmallParam))
            return Result.newInstance().setCode(200).setMsg("删除成功");
        return Result.newInstance().setCode(500).setMsg("删除失败");
    }


    /**
     * 根据小类id查询小类信息
     *
     * @param id 小类信息id
     * @return 根据小类id查询的小类信息数据
     */
    @GetMapping("/getSmallById/{id}")
    @ApiOperation("单个查询小类信息")
    public Result getSmallById(@PathVariable("id") Integer id) {
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsSmallService.getById(id));
    }

    /**
     * 查询所有二级类信息
     * @return
     */
    @GetMapping("/listAllSmall")
    @ApiOperation("查询所有二级类信息")
    public Result listAllSmall() {
        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(goodsSmallService.list());
    }


}

